package com.doform.android.xianggucheng.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.doform.android.xianggucheng.IApplication;
import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.ErCodeActivity;
import com.doform.android.xianggucheng.activity.ListArticleActivity;
import com.doform.android.xianggucheng.activity.ListCategoryActivity1;
import com.doform.android.xianggucheng.activity.ListClientActivity;
import com.doform.android.xianggucheng.activity.ListClientCategoryActivity;
import com.doform.android.xianggucheng.activity.ListMeasureUnitActivity;
import com.doform.android.xianggucheng.activity.ListPriceStarActivity;
import com.doform.android.xianggucheng.activity.ListVendorActivity;
import com.doform.android.xianggucheng.activity.ReportActivity;
import com.doform.android.xianggucheng.activity.SalesActivity;
import com.doform.android.xianggucheng.activity.downorder.ManGoodsActivity;
import com.doform.android.xianggucheng.activity.downorder.OrderQueryActivity;
import com.doform.android.xianggucheng.activity.library.ListInLibraryActivity;
import com.doform.android.xianggucheng.activity.login.MaLoginActivity;
import com.doform.android.xianggucheng.bean.HomePager;
import com.doform.android.xianggucheng.bean.request_bean.ReqHome;
import com.doform.android.xianggucheng.help.AppHelp;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.AppUtils;
import com.doform.android.xianggucheng.utils.SP;
import com.doform.android.xianggucheng.widget.DividerGridItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-07-15.
 * GitHub：https://github.com/cxydxpx
 */

public class HomeFragment extends BaseFragment {

    //    总单数
    @BindView(R.id.tv_total_num)
    TextView tv_total_num;

    //    待发货
    @BindView(R.id.tv_no_num)
    TextView tv_no_num;

    //    已发货
    @BindView(R.id.tv_yes_num)
    TextView tv_yes_num;

    //    营业额
    @BindView(R.id.tv_sales)
    TextView tv_sales;

    //    实收
    @BindView(R.id.tv_shishow)
    TextView tv_shishow;

    @BindView(R.id.tv_name)
    TextView tv_name;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private List<String> mDatas;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initViews() {

        //设置布局管理器
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(mContext));

    }

    @Override
    protected void initData() {
        mDatas = new ArrayList<>();

        switch (AppHelp.getInstance().getUserType()) {
            case 1://店铺
                mDatas.add("下单");
                mDatas.add("订单");
                mDatas.add("经营报表");
                mDatas.add("销售报表");
                mDatas.add("商品管理");
                mDatas.add("商品分类");
                mDatas.add("供应商管理");
                mDatas.add("客户管理");
                mDatas.add("客户分类");
                mDatas.add("价格星级");
                mDatas.add("采购入库");
                mDatas.add("计量单位");
                mDatas.add("二维码扫描");
                mDatas.add("安全退出");
                break;
            case 2://小工
                mDatas.add("订单");
                mDatas.add("安全退出");
                break;
            case 3://销售报表
                mDatas.add("安全退出");
                break;
            default:
//                解决1.0未设置权限的bug  临时
                mDatas.add("下单");
                mDatas.add("订单");
                mDatas.add("经营报表");
                mDatas.add("销售报表");
                mDatas.add("商品管理");
                mDatas.add("商品分类");
                mDatas.add("供应商管理");
                mDatas.add("客户管理");
                mDatas.add("客户分类");
                mDatas.add("价格星级");
                mDatas.add("采购入库");
                mDatas.add("计量单位");
                mDatas.add("二维码扫描");
                mDatas.add("安全退出");

                break;
        }
    }

    @Override
    public void initListener() {
        final CommonAdapter<String> mCommonAdapter = new CommonAdapter<String>(mContext, R.layout.fragment_home_item, mDatas) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {

                TextView btn = holder.getView(R.id.btn_edit);

                switch (s) {
                    case "下单":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.place_order), null, null);
                        break;
                    case "订单":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.order), null, null);
                        break;
                    case "经营报表":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.report), null, null);
                        break;
                    case "销售报表":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.sale), null, null);
                        break;
                    case "商品管理":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.sp_manager), null, null);
                        break;
                    case "商品分类":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.sp_category), null, null);
                        break;
                    case "供应商管理":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.vendor_manager), null, null);
                        break;
                    case "客户管理":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.client_manager), null, null);
                        break;
                    case "客户分类":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.client_category), null, null);
                        break;
                    case "价格星级":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.client_star), null, null);
                        break;
                    case "采购入库":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.library), null, null);
                        break;
                    case "计量单位":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.unit), null, null);
                        break;
                    case "二维码扫描":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.ew_code), null, null);
                        break;
                    case "安全退出":
                        btn.setCompoundDrawablesWithIntrinsicBounds(null, AppUtils.resourcesTodrawable(mContext, R.mipmap.sign_out), null, null);
                        break;
                    default:
                        break;
                }
                btn.setText(s);

            }
        };

        mCommonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

                switch (mDatas.get(position)) {
                    case "下单":
                        startActivity(new Intent(mContext, ManGoodsActivity.class));
                        break;
                    case "订单":
                        startActivity(new Intent(mContext, OrderQueryActivity.class));
                        break;
                    case "经营报表":
                        startActivity(new Intent(mContext, ReportActivity.class));
                        break;
                    case "销售报表":
                        startActivity(new Intent(mContext, SalesActivity.class));
                        break;
                    case "商品管理":
                        startActivity(new Intent(mContext, ListArticleActivity.class));
                        break;
                    case "商品分类":
                        startActivity(new Intent(mContext, ListCategoryActivity1.class));
                        break;
                    case "供应商管理":
                        startActivity(new Intent(mContext, ListVendorActivity.class));
                        break;
                    case "客户管理":
                        startActivity(new Intent(mContext, ListClientActivity.class));
                        break;
                    case "客户分类":
                        startActivity(new Intent(mContext, ListClientCategoryActivity.class));
                        break;
                    case "价格星级":
                        startActivity(new Intent(mContext, ListPriceStarActivity.class));
                        break;
                    case "计量单位":
                        startActivity(new Intent(mContext, ListMeasureUnitActivity.class));
                        break;
                    case "采购入库":
                        startActivity(new Intent(mContext, ListInLibraryActivity.class));
                        break;
                    case "二维码扫描":
                       startActivity(new Intent(mContext, ErCodeActivity.class));
                        break;
                    case "安全退出":
                        exitBefor();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(mCommonAdapter);
    }

    @Override
    protected void updateData() {
        super.updateData();
        getDataForService();
    }

    private void getDataForService() {
        HttpManager
                .instance()
                .getHomePagerData(new ReqHome())
                .subscribe(new ErrorHandleSubscriber<HomePager>() {
                               @Override
                               public void onNext(@NonNull HomePager homePager) {
                                   showData(homePager.getList());
                               }
                           }
                );
    }

    private void showData(HomePager.ListBean data) {

        AppHelp.getInstance().setIsFinancial(data.getIs_financial());

        tv_name.setText(data.getShop_name());
        tv_total_num.setText(data.getTotal_num() + "单");
        tv_no_num.setText("待发货 : " + data.getNot_send() + "单");
        tv_yes_num.setText("已发货 : " + data.getHas_send() + "单");
        tv_sales.setText("￥" + data.getTotal_fee());
        tv_shishow.setText("￥" + data.getHaspay_fee());
    }

    private void exitBefor() {
        new AlertDialog
                .Builder(mContext)
                .setMessage("确定要退出吗?")
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                exit();
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                .show();
    }

    private void exit() {
        SP.clear(mContext);
        IApplication.getInstance().destory();
        startActivity(new Intent(mContext, MaLoginActivity.class));
        getActivity().finish();
    }


}
